<?php

class Application_Model_ColorMapper {

    protected $_dbTable;

    public function getDbTable() {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Color');
        }
        return $this->_dbTable;
    }

    public function setDbTable($dbTable) {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Ungueltige Gateway-Klasse fuer Tabellendaten');
        }
        $this->_dbTable = $dbTable;
    }

    public function save(Application_Model_ColorModel $color) {
        $data = array(
            'id' => $color->getId(),
            'hex' => $color->getHex(),
            'name_en' => $color->getName_en(),
            'name_de' => $color->getName_de()
        );
        if (null === $color->getId()) {
            unset($data['id']);

            $color->setId($this->getDbTable()->insert($data));
        } else {
            $this->getDbTable()->update($data, array('id = ?' => $data['id']));
        }
        return $this;
    }

    public function find($id, Application_Model_ColorModel $color) {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return null;
        }
        $row = $result->current();
        $color->setId($row['id'])
                ->setHex($row['hex'])
                ->setName_en($row['name_en'])
                ->setName_de($row['name_de']);

        return $color;
    }

    public function fetchAll($where = null, $order = null, $count = null, $offset = null) {
        $resultSet = $this->getDbTable()->fetchAll($where, $order, $count, $offset);
        $entries = array();
        foreach ($resultSet as $row) {
            $entry = new Application_Model_ColorModel();
            $entry->setId($row['id'])
                    ->setHex($row['hex'])
                    ->setName_en($row['name_en'])
                    ->setName_de($row['name_de']);
            $entries[] = $entry;
        }
        return $entries;
    }

    public function fetchFree($where = null, $order = null, $count = null, $offset = null) {
        $colorTable = new Application_Model_DbTable_Color();
        $query = $colorTable->select()->setIntegrityCheck(false);
        $query->from(array('c' => 'color'), array('c.id', 'c.hex', 'c.name_en', 'c.name_de'));
        $query->join(array('u' => 'user'), 'c.hex = u.color', array());
        $query->where('u.color IS NULL');
        $results = $colorTable->fetchAll($query);
        return $results;
       /* 
     $results = $accountTable->fetchAll($query);
        
        
        $resultSet = $this->getDbTable()->fetchAll($where, $order, $count, $offset);
        $entries = array();
        foreach ($resultSet as $row) {
            $entry = new Application_Model_ColorModel();
            $entry->setId($row['id'])
                    ->setHex($row['hex'])
                    ->setName_en($row['name_en'])
                    ->setName_de($row['name_de']);
            $entries[] = $entry;
        }
        return $entries;
        
        */
    }

    public function delete($colorId) {
        if ($colorId instanceof Application_Model_ColorModel) {
            $colorId = $colorId->id;
        }
        return $this->getDbTable()->delete(array('id = ?' => $colorId));
    }

}
